const cloud = require('wx-server-sdk');
cloud.init();

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();
  try {
    let page_url =
      '/pages/sure_buy/sure_buy?device=2&out_trade_no=' +
      event.out_trade_no;
    if (event.curriculum_id != undefined) {
      page_url += '&curriculum_id=' + event.curriculum_id;
    }
    const result = await cloud.openapi.templateMessage.send({
      touser: wxContext.OPENID,
      page: page_url,
      data: {
        keyword1: {
          value: event.order_time
        },
        keyword2: {
          value: event.out_trade_no
        },
        keyword3: {
          value: event.name
        },
        keyword4: {
          value: '￥' + event.total_fee
        },
        keyword5: {
          value: '￥' + event.total_fee
        },
        keyword6: {
          value: '请在' + event.deadline_time + '前完成支付'
        }
      },
      templateId: 'wd_Xmld5yt3PsZfAYfJx9AkNBzcxmRSyTMX6tYeivOI',
      formId: event.form_id
    });
    console.log(JSON.stringify(event));
    return result;
  } catch (err) {
    console.log(err);
    return err;
  }
};
